﻿#include "balserform1.h"
#include "ui_balserform1.h"
#include<QDebug>
#include <QMessageBox>
#include <pylon/gige/BaslerGigEInstantCamera.h>

static const uint32_t c_countOfImagesToGrab = 1;
using namespace std;
using namespace Basler_GigECameraParams;


balserForm1::balserForm1(QString cameraName, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::balserForm1)
{
    ui->setupUi(this);
    m_cameraName = cameraName;
    cam = new CcameraConnectBasler(m_cameraName);
    isFindCam = cam->findCamera();   //若正常连接，此项必须要。。
//    isFindCam == false;            //测试使用
    if (isFindCam == true)
    {
        if (cam->connect())
        {
            connect(ui->spbExpose, SIGNAL(valueChanged(int)), this, SLOT(doProSetExpTime(int)));
            connect(ui->spbGainRaw, SIGNAL(valueChanged(int)), this, SLOT(doProSetGainRaw(int)));
        }
    }
}

balserForm1::~balserForm1()
{
    delete ui;
}

void balserForm1::setProjectName(int camera, QString curProjectName)
{
    m_curCam = camera;
    m_curProjectName = curProjectName;
}

bool balserForm1::connectCam()
{
    if (isFindCam == true)
        return cam->connect();
    else return 0; //0为未连接
}

void balserForm1::grabOneFrame(Hobject *image)
{
    if (isFindCam == false) return;
    cam->grabOneFrame(image);

}

void balserForm1::setCamPara(int schedule)
{
    if (isFindCam == false) return;
    if (readData(schedule))
    {
        if (schedule == 1)
        {
            cam->setExposureTime(m_para.expTime1);
            cam->setGainRaw(m_para.gainRaw1);
        }
        else if (schedule == 2)
        {
            cam->setExposureTime(m_para.expTime2);
            cam->setGainRaw(m_para.gainRaw2);
        }
    }
}

void balserForm1::setUiPara(int schedule)
{
    if (isFindCam == false) return;
    if (readData(schedule))
    {
        if (schedule == 1)
        {
            ui->spbExpose->setValue(m_para.expTime1);
            ui->spbGainRaw->setValue(m_para.gainRaw1);
        }
        else if (schedule == 2)
        {
            ui->spbExpose->setValue(m_para.expTime2);
            ui->spbGainRaw->setValue(m_para.gainRaw2);
        }
    }
}

bool balserForm1::saveData(int cam, int schedule)
{
    if (isFindCam == true)
    {
        saveData2Ini(m_curProjectName, "cammerID" + QString::number(cam) + QString::number(schedule), "expTime", ui->spbExpose->value());
        saveData2Ini(m_curProjectName, "cammerID" + QString::number(cam) + QString::number(schedule), "gainRaw", ui->spbGainRaw->value());
        return true;
    }
    return false;
}

bool balserForm1::readData(int schedule)
{
    if (isFindCam == true)
    {
        QVariant midValue;
        if (schedule == 1)
        {
            readData2Ini(m_curProjectName, "cammerID" + QString::number(m_curCam) + QString::number(schedule), "expTime", &midValue);
            m_para.expTime1 = midValue.toInt();
            readData2Ini(m_curProjectName, "cammerID" + QString::number(m_curCam) + QString::number(schedule), "gainRaw", &midValue);
            m_para.gainRaw1 = midValue.toInt();
        }
        else if (schedule == 2)
        {
            readData2Ini(m_curProjectName, "cammerID" + QString::number(m_curCam) + QString::number(schedule), "expTime", &midValue);
            m_para.expTime2 = midValue.toInt();
            readData2Ini(m_curProjectName, "cammerID" + QString::number(m_curCam) + QString::number(schedule), "gainRaw", &midValue);
            m_para.gainRaw2 = midValue.toInt();
        }
        return true;
    }
    return false;
}



void balserForm1::timerEvent(QTimerEvent *event)
{
    if (event->timerId() == m_timeid)
    {
        cam->getOneFrame(&m_matchImage);
        emit camGrabOneResult(m_matchImage);
    }
}

void balserForm1::closeEvent(QCloseEvent *event)
{
    on_btnstop_clicked();
}

void balserForm1::on_btngrabOne_clicked()
{
    if (isFindCam == true)
    {
        cam->grabOneFrame(&m_matchImage);

        emit camGrabOneResult(m_matchImage);
    }
    else
        QMessageBox::warning(NULL, QStringLiteral("错误"), QStringLiteral("相机未连接,请先连接相机！"));

}

void balserForm1::on_btnstop_clicked()
{
    if (isFindCam == true)
    {
        killTimer(m_timeid);
        cam->stopGrabFrame();
        ui->btngrabOne->setEnabled(true);
        ui->btngrabContinue->setEnabled(true);
        ui->btnstop->setEnabled(false);
    }
}

void balserForm1::on_btngrabContinue_clicked()
{
    if (isFindCam == true)
    {
        if (cam->openGrabFrame())
        {
            m_timeid = startTimer(200);
        }
        ui->btngrabOne->setEnabled(false);
        ui->btngrabContinue->setEnabled(false);
        ui->btnstop->setEnabled(true);
    }
    else
        QMessageBox::warning(NULL, QStringLiteral("错误"), QStringLiteral("相机未连接,请先连接相机！"));
}

void balserForm1::on_btnOK_clicked()
{
    this->close();
}

void balserForm1::doProSetExpTime(int value)
{
    qDebug() << value;
    cam->setExposureTime(value);
}

void balserForm1::doProSetGainRaw(int value)
{
    qDebug() << value;
    cam->setGainRaw(value);
}

void balserForm1::on_btnCancel_clicked()
{
    this->close();
}
